<?php

namespace app\command;

use EasyWeChat\Factory;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Log;

class Express extends Command
{

    protected function configure()
    {
        // 指令配置
        $this->setName('Express');
        // 设置参数
        $this->setDescription('获取快递公司列表')->setHelp("php think Express");
    }


    protected function execute(Input $input, Output $output)
    {
        $app = Factory::miniProgram(config('wechat.xcx'));
        $result = $app->express->listProviders();
        if($result['count']  == 0) return;
        $data = [];
        foreach ($result['data'] as $provider){
            $express = (new \app\admin\model\Express())->where(['delivery_id' => $provider['delivery_id']])->find();
            if($express){
                continue;
            }
            $data[] = [
               'delivery_id' => $provider['delivery_id'],
                'delivery_name' => $provider['delivery_name'],
                'create_time' => time()
            ];
        }

        if($data){
            (new \app\admin\model\Express())->insertAll($data);
            Log::channel('command')->info("新增成功".json_encode($data));
        }
        $output->writeln('执行结束');
    }

}